<?php
class DownloadsController extends AppController {

    public $components = array('SwfUpload');
    public $helpers = array('Pagination','Number');

    public function index() {
        $this->Download->recursive = 0;
        $this->set('downloads', $this->paginate());
    }

    public function view($file = null) {
        $file = $this->Download->findByFile($file);
        if (empty($file)) {
            $this->Session->setFlash(__('File not found', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('file',$file);
    }

    public function get($file) {
        $name = $this->Download->findByFile($file,'file');
        if(!empty($name)) {
            $this->Download->updateAll(array('downloaded'=>'downloaded+1'),array('Download.file'=>$file));
            App::import('vendor','file');
            $path = 'files'.DS.'downloads'.DS.$name['Download']['file'];
            $file =& new File($path);
            $this->RequestHandler->respondAs('file');
            echo $file->read();
        }
    }

    public function admin_index() {
        $this->Download->recursive = 0;
        $this->set('downloads', $this->paginate());
    }

    public function admin_add() {
        if(!empty($this->data)) {
            $this->SwfUpload->uploadpath = 'files' . DS . 'downloads' . DS;
            $this->SwfUpload->data['Download']['Filedata'] = $this->data['Download']['file'];

            if($this->SwfUpload->upload()) {
                $this->data = $this->Download->prepare_file_data($this->data,$this->SwfUpload);
                $this->Download->create();
                if($this->Download->save($this->data)) {
                    $this->Session->setFlash(__('The File has been saved',true));
                    $this->redirect(array('action'=>'index'));
                } else {
                    $this->Session->setFlash(__('The File could not be saved. Please, try again.',true));
                }
            } else {
                $this->Session->setFlash(__('The File could not be saved. Please, try again.',true));
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$this->Download->findById($id)) {
            $this->Session->setFlash(__('Invalid file', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Download->save($this->data)) {
                $this->Session->setFlash(__('The File has been saved',true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The File could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->Download->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
        if ($this->Download->delete_file($id)) {
            $this->Download->del($id);
            $this->Session->setFlash(__('File deleted', true));
        } else {
            $this->Session->setFlash(__('The File could not be deleted. Please, try again.', true));
        }
        $this->redirect(array('action'=>'index'));
    }

    public function admin_delete_files() {
        if ($this->Download->delete_files($this->data['DownloadsDelete'])) {
            $this->Session->setFlash(__('Files deleted',true));
        } else {
            $this->Session->setFlash(__('The Files could not be deleted. Please, try again', true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function sidebox() {
        return $this->Download->last();
    }
}
?>